package cn.tedu.jdbc;

import cn.tedu.jdbc.utils.DbUtils;

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;

/**
 * 使用PreparedStatement向数据库表中插入一张图片
 */
public class JDBCTest17 {
    public static void main(String[] args) {
        Connection conn = null;
        PreparedStatement ps = null;
        FileInputStream in = null;

        try {
            conn = DbUtils.getConnection();
            String sql = "insert into t_img(name,img) values(?, ?)";
            ps = conn.prepareStatement(sql);
            ps.setString(1,"640.jpg");
            in = new FileInputStream("D:\\Soft_Install\\JAVA\\JSD2503\\JDBC\\640.jpg");
            ps.setBlob(2,in);
            int count = ps.executeUpdate();
            System.out.println("插入了" + count + "条记录");


        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            if (in != null){
                try {
                    in.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }

            }
            DbUtils.close(conn,ps,null);
        }
    }
}
